/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Home
 */
public class Customer {
    //InfoDatabase idb = DBUtil.getInfoDB();

    public static boolean insertCustomer(Entity.Customer cus) {
        String sql = "insert into Customers values(?, ?, ?, ?, ?)";
        try {
            PreparedStatement pst = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);
            pst.setString(1, cus.getCustomerName());
            pst.setString(2, cus.getAddress());
            pst.setString(3, cus.getPhone());
            pst.setString(4, cus.getEmail());
            pst.setBoolean(5, cus.isGender());
            int i = pst.executeUpdate();
            if (i == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public static boolean updateCustomer(Entity.Customer cus) {
        String sql = "update Customers set CustomerName=?, Address=?, Phone=?, Email=?, Gender=? where CustomerID=?";
        try {
            PreparedStatement pst = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);
            pst.setString(1, cus.getCustomerName());
            pst.setString(2, cus.getAddress());
            pst.setString(3, cus.getPhone());
            pst.setString(4, cus.getEmail());
            pst.setBoolean(5, cus.isGender());
            pst.setInt(6, cus.getCustomerID());
            int i = pst.executeUpdate();
            if (i == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

//    public static boolean delCustomer(Entity.Customer cus)
//    {
//        String sql = "update Customers set Status=? where CustomerID=?";
//        try {
//            PreparedStatement pst = DBUtil.getConnection(DBUtil.getInfoDB()).prepareCall(sql);
//            pst.setBoolean(1, cus.isStatus());
//            pst.setInt(2, cus.getCustomerID());
//            int i = pst.executeUpdate();
//            if(i == 1){
//                return true;
//            }else{
//                return false;
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
//            return false;
//        }
//    }
    public static boolean deleteCustomer(int id) {
        String sql = "delete from Customers where CustomerID=" + id;
        Statement st;
        try {
            st = DBUtil.getConnection(DBUtil.getInfoDB()).createStatement();
            int i = st.executeUpdate(sql);
            if(i == 1){
                return true;
            }else{
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            return true;
        }
    }

    public static ArrayList<Entity.Customer> getAllCustomer() {
        ArrayList<Entity.Customer> list = null;
        try {
            list = new ArrayList<Entity.Customer>();
            String sql = "select * from Customers";
            Statement st = DBUtil.getConnection(DBUtil.getInfoDB()).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                Entity.Customer cus = new Entity.Customer();
                cus.setCustomerID(rs.getInt("CustomerID"));
                cus.setCustomerName(rs.getString("CustomerName"));
                cus.setAddress(rs.getString("Address"));
                cus.setPhone(rs.getString("Phone"));
                cus.setEmail(rs.getString("Email"));
                cus.setGender(rs.getBoolean("Gender"));
                list.add(cus);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
}
